<template>
    <view class="spec-box">
        <view
            ><text class="s-tit">{{ title }}</text
            ><text v-if="unit">{{ titleTxt }}</text></view
        >
        <view class="s-desc" v-if="unit">{{ titleDesc }}</view>
        <view class="l-box">
            <view class="unit-box">
                <view
                    class="unit-item"
                    :class="item.checked ? 'selected' : ''"
                    v-for="(item, index) in list"
                    @click="clickItem(item)"
                    :key="index"
                >
                    {{ item.title }}
                </view>
            </view>
        </view>
        <!-- 最小单位选择 -->
        <view class="item-box" v-show="unit && seleList.length > 0">
            <dropdowMenu
                ref="dropdowMenu"
                :descObj="descObj"
                :list="seleList"
                @changeMenu="changeMenu"
            ></dropdowMenu>
        </view>
        <!-- 换算 -->
        <view class="cover-box" v-show="seleList.length > 1">
            <view class="itemd-box" v-for="item in coverList" :key="item.id">
                <comConvert
                    v-show="type == 1 && !item.isMin"
                    @changInputTxt="changInputTxt"
                    :id="item.id"
                    :titleTxt="'换算'"
                    :title="'1' + item.name_rel + '='"
                    :covernum="item.covernum"
                    :minUnil="minUnit ? minUnit.name_rel : ''"
                >
                </comConvert>
            </view>
        </view>
    </view>
</template>

<script>
import comDorpDown from "./ComDorpDown.vue";
import comConvert from "./ComConvert.vue";
import dropdowMenu from "./DropdownMenu.vue";
import lang from "../../js/conf/Lang.js";
import goodsCtrl from "../../js/gd/GoodsCtrl.js";
import projectCtrl from "../../js/project/GoodsCtrl.js";

export default {
    components: { comDorpDown, comConvert, dropdowMenu },
    props: ["type", "unit", "is_good_type"],
    data() {
        return {
            title: lang.unitTxt.title,
            titleTxt: lang.unitTxt.titleTxt,
            is_good: this.is_good_type ? this.is_good_type : 1,
            titleDesc: "",
            descObj: null,
            list: [],
            seleList: [],
            coverList: [],
            minUnit: null,
            minUnitTxt: "",
            oldItem: null,
        };
    },
    methods: {
        /***初始化数据***/
        initData() {
            if (this.type == 1) {
                //单位
                this.clearData();
                this.initUnitData();
            }
        },

        clearData() {
            this.titleDesc = "";
            this.descObj = null;
            this.list = [];
            this.seleList = [];
            this.coverList = [];
            this.minUnit = null;
            this.minUnitTxt = "";
        },

        /****初始化单位数据****/
        initUnitData() {
            this.clearData();
            if (this.is_good == 1) {
                this.list = goodsCtrl.getUnitList();
            } else {
                this.list = projectCtrl.getUnitList();
            }
            this.title = lang.unitTxt.title;
            this.titleTxt = lang.unitTxt.titleTxt;
            this.titleDesc = "";
            this.descObj = {};
            this.descObj.isHasTit = true;
            this.descObj.titleTxt = lang.unitTxt.minTitle;
            if (this.is_good == 1) {
                this.seleList = goodsCtrl.getSelectedUnit();
                this.coverList = goodsCtrl.getUnitCover();
            } else {
                this.seleList = projectCtrl.getSelectedUnit();
                this.coverList = projectCtrl.getUnitCover();
            }

            if (this.list && this.list.length > 0) {
                //没有单位默认第一个单位
                if (this.seleList && this.seleList.length > 0) {
                    // for(let i = 0; i< this.seleList.length;i++)
                    // {
                    // 	let obj =  this.seleList[i];
                    // 	if(obj.checked)
                    // 	{
                    // 		item = JSON.parse(JSON.stringify(obj));
                    // 		item.checked = false;
                    // 		break;
                    // 	}
                    // }
                } else {
                    let item = this.list[0];
                    this.clickItem(item);
                }
                // this.changeUnit(item);
                // this.$emit("changeUnit");
            }

            if (this.is_good == 1) {
                this.minUnit = goodsCtrl.minUnit;
            } else {
                this.minUnit = projectCtrl.minUnit;
            }
            this.setUnitTitleDesc();
            if (this.minUnit) {
                this.descObj.title = this.minUnit.name_rel; //最小单位title;
                this.descObj.id = this.minUnit.id;
            }
        },

        /*******改变单位******/
        changeUnit(item) {
            if (!this.unit) this.allUnChecked(item.id);
            let res =
                this.is_good == 1
                    ? goodsCtrl.setSelectedUnit(item)
                    : projectCtrl.setSelectedUnit(item);
            if (res) {
                item.checked = !item.checked;
            }

            this.$refs.dropdowMenu.openMenu();
            if (this.is_good == 1) {
                this.seleList = goodsCtrl.getSelectedUnit();

                this.coverList = goodsCtrl.getUnitCover();

                this.minUnit = goodsCtrl.minUnit;
            } else {
                this.seleList = projectCtrl.getSelectedUnit();

                this.coverList = projectCtrl.getUnitCover();

                this.minUnit = projectCtrl.minUnit;
            }

            this.setUnitTitleDesc();
            this.descObj.title = this.minUnit.name_rel;
            this.descObj.id = this.minUnit.id;
            this.$refs.dropdowMenu.openMenu();
            this.$forceUpdate();
        },

        allUnChecked(id) {
            if (this.list) {
                let len = this.list.length;
                for (let i = 0; i < len; i++) {
                    let obj = this.list[i];
                    if (id == obj.id) continue;
                    obj.checked = false;
                }
            }
        },

        /***改变最小单位****/
        changeMinUnit(item) {
            this.descObj.isHasTit = true;
            this.descObj.titleTxt = lang.unitTxt.minTitle;
            this.descObj.title = item.title;
            let obj = {};
            obj.covernum = 0;
            obj.id = item.id;
            (obj.name = ""), (obj.isMin = true);
            obj.name_rel = item.title;
            item.isMin = true;
            if (this.is_good == 1) {
                goodsCtrl.setMinUnit(obj);
                this.minUnit = goodsCtrl.minUnit;

                goodsCtrl.setUnitCoverArrIsMin(item.id);
                this.coverList = goodsCtrl.getUnitCover();
            } else {
                projectCtrl.setMinUnit(obj);
                this.minUnit = projectCtrl.minUnit;

                projectCtrl.setUnitCoverArrIsMin(item.id);
                this.coverList = projectCtrl.getUnitCover();
            }

            this.setUnitTitleDesc();
            if (this.minUnit) {
                this.descObj.title = this.minUnit.name_rel;
                this.descObj.id = this.minUnit.id;
            }
        },

        /****设置单位换算说明文字****/
        setUnitTitleDesc() {
            let len = this.coverList.length;
            this.titleDesc = "";
            for (let i = 0; i < len; i++) {
                let obj = this.coverList[i];
                if (obj.isMin) {
                    continue;
                }
                if (this.is_good == 1) {
                    if (goodsCtrl.minUnit) {
                        let str =
                            " 1 " +
                            obj.name_rel +
                            " = " +
                            obj.covernum +
                            " " +
                            goodsCtrl.minUnit.name_rel;
                        if (this.titleDesc == "") {
                            this.titleDesc = str;
                        } else {
                            this.titleDesc = this.titleDesc + ", " + str;
                        }
                    }
                } else {
                    if (projectCtrl.minUnit) {
                        let str =
                            " 1 " +
                            obj.name_rel +
                            " = " +
                            obj.covernum +
                            " " +
                            projectCtrl.minUnit.name_rel;
                        if (this.titleDesc == "") {
                            this.titleDesc = str;
                        } else {
                            this.titleDesc = this.titleDesc + ", " + str;
                        }
                    }
                }
            }
        },

        /**点击选择单位***/
        clickItem(item) {
            if (this.type == 1) {
                //单位
                if (item.checked) {
                    let count = this.seleList.length;
                    if (count == 1) {
                        this.$msg({ title: lang.unitTxt.oneUnitTip });
                        return;
                    }
                }

                this.changeUnit(item);
                this.$emit("changeUnit", this.type);
            }
        },

        /***菜单改变***/
        changeMenu(item) {
            if (this.type == 1) {
                //单位
                this.changeMinUnit(item);
                if (this.is_good == 1) {
                    // goodsCtrl.changeUnitPriceCover(this.changeUnitPriceCoverCallBck);
                    goodsCtrl.qucklyFillPrice(this.changeUnitCallBck, false);
                    goodsCtrl.qucklyFillCost(false);
                } else {
                    // projectCtrl.changeUnitPriceCover(this.changeUnitPriceCoverCallBck);
                    projectCtrl.qucklyFillPrice(this.changeUnitCallBck, false);
                    projectCtrl.qucklyFillCost(false);
                }
            }
        },

        /***改变换算数值***/
        changInputTxt(data) {
            if (this.is_good == 1) {
                goodsCtrl.changUnitCover(data);
                this.coverList = goodsCtrl.getUnitCover();
                this.setUnitTitleDesc();
                goodsCtrl.qucklyFillPrice(this.changeUnitCallBck, false);
                // goodsCtrl.changeUnitPriceCover(this.changeUnitPriceCoverCallBck);
                goodsCtrl.qucklyFillCost(false);
            } else {
                projectCtrl.changUnitCover(data);
                this.coverList = projectCtrl.getUnitCover();
                this.setUnitTitleDesc();
                projectCtrl.qucklyFillPrice(this.changeUnitCallBck, false);
                // projectCtrl.changeUnitPriceCover(this.changeUnitPriceCoverCallBck);
                projectCtrl.qucklyFillCost(false);
            }
        },

        changeUnitCallBck(data) {
            this.$emit("changeUnitPriceCover", data);
        },
    },
};
</script>

<style lang="scss">
.spec-box {
    font-size: 13px;
    color: #333;
    padding-top: 20px;
    padding-bottom: 20px;
    .s-tit {
        font-weight: bold;
    }
    .l-box {
        width: 100%;
        display: flex;
        margin-bottom: 10px;
        .unit-box {
            display: flex;
            flex-wrap: wrap;
            width: 100%;
            .unit-item {
                padding: 4px 20px;
                border-radius: 4px;
                margin-right: 10px;
                margin-top: 10px;
                border: 1px solid #3388ff;
                cursor: pointer;
                color: #333;
            }
            .selected {
                background-color: #3388ff;
                color: #fff;
            }
        }
    }
    .cover-box {
        display: flex;
        flex-wrap: wrap;
        width: 100%;
        .item-box {
            padding-right: 10px;
        }
    }
}
</style>
